package com.example.myrecycledemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import static android.view.KeyEvent.KEYCODE_DPAD_DOWN;

public class MainActivity extends AppCompatActivity {

    private ArrayList<Model> arrayList;
    private MyRecycleAdapter adapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        arrayList = new ArrayList();
        for(int i=0;i<=10;i++){
            Model model=new Model();
            model.setName("Demo---"+i);
            model.setId(R.mipmap.ic_launcher);
            arrayList.add(model);
        }
        recyclerView = (RecyclerView) findViewById(R.id.recycle);
        recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(),1, GridLayoutManager.VERTICAL,false));
        adapter = new MyRecycleAdapter();
        recyclerView.setAdapter(adapter);


    }


    class Model{
        String name;
        int id;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }
    }


    int focusPosition;

    private class MyRecycleAdapter extends RecyclerView.Adapter<MyRecycleAdapter.ViewHolder>{

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view=View.inflate(getApplicationContext(),R.layout.item,null);
            view.setFocusable(true);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final ViewHolder holder, final int position) {

            holder.imageView.setImageResource(arrayList.get(position).getId());
            holder.textView.setText(arrayList.get(position).getName());

            holder.itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if(hasFocus){
                        holder.relative.setBackgroundResource(R.drawable.image1228);
                        focusPosition=position;
                    }else{
                        holder.relative.setBackgroundResource(R.mipmap.ic_launcher);
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }



        class ViewHolder extends RecyclerView.ViewHolder{
            ImageView imageView;
            RelativeLayout relative;
            TextView textView;
            public ViewHolder(View itemView) {
                super(itemView);
                imageView= (ImageView) itemView.findViewById(R.id.item_image);
                relative= (RelativeLayout) itemView.findViewById(R.id.relative);
                textView= (TextView) itemView.findViewById(R.id.textView);
            }
        }
    }


    int j=0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KEYCODE_DPAD_DOWN:
                if(focusPosition==arrayList.size()-1){
                    j++;
                    final int dataSize=arrayList.size();
                    for(int i=0;i<=5;i++){
                        Model model=new Model();
                        model.setName("Demo---"+j+"more------"+i);
                        model.setId(R.mipmap.ic_launcher);
                        arrayList.add(model);
                    }
                    Log.i("sufutian==", "onKeyDown: "+"notifyDataSetChanged");
                    adapter.notifyDataSetChanged();


                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            recyclerView.smoothScrollToPosition(dataSize);
                            if(recyclerView.getChildAt(dataSize)!=null){
                                recyclerView.getChildAt(dataSize).requestFocus();
                            }
                        }
                    },200);
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
